<?php
/***********************************************************
	Filename: libs/helper.sys.php
	Note	: 辅助函数，均以sys_开头，在任何应用中都会载入
	Version : 3.0
	Author  : qinggan
	Update  : 2009-10-19
***********************************************************/
//自动跳转
if(!function_exists("sys_header"))
{
	function sys_header($url)
	{
		if(!headers_sent())
		{
			header("Location:".$url);
		}
		else
		{
			echo '<script type="text/javascript">';
			echo 'window.location.href="'.$url.'";';
			echo '</script>';
		}
		exit;
	}
}

//安全数据传输
if(!function_exists("sys_rgpc_safe"))
{
	function sys_rgpc_safe($msg,$f=false)
	{
		$status = get_magic_quotes_gpc();
		if(!$status || $f)
		{
			if(is_array($msg))
			{
				foreach($msg AS $key=>$value)
				{
					$msg[$key] = sys_rgpc_safe($value,$f);
				}
			}
			else
			{
				$msg = addslashes($msg);
			}
		}
		return $msg;
	}
}

//统计运行时间
if(!function_exists("sys_time_used"))
{
	function sys_time_used()
	{
		if(!defined("SYS_TIME_START"))
		{
			return false;
		}
		$time = explode(" ",microtime());
		$end_time = $time[0] + $time[1];
		return round(($end_time-SYS_TIME_START),5);
	}
}

//内存应用统计
if(!function_exists("sys_memory_used"))
{
	function sys_memory_used()
	{
		if(!defined("SYS_MEMORY_START") || !function_exists("memory_get_usage"))
		{
			return false;
		}
		$end = memory_get_usage();
		$used = $end - SYS_MEMORY_START;
		if($used <= 1024)
		{
			$used = "1 KB";
		}
		elseif($used>1024 && $used<(1024*1024))
		{
			$used = round(($used/1024),4)." KB";
		}
		else
		{
			$used = round(($used/(1024*1024)),4)." MB";
		}
		return $used;
	}
}


//获取控制层的三个参数
if(!function_exists("sys_get_cf"))
{
	function sys_get_cf($var)
	{
		$val = $_GET[$var];
		if(!$val)
		{
			return false;
		}
		//判断格式是否正确
		if(!ereg('^[a-z][a-z\_0-9]+$',$val))
		{
			exit('error: Only letters, numbers and underscores!');
		}
		return $val;
	}
}


if(!function_exists("sys_get_d"))
{
	function sys_get_d($var)
	{
		$val = $_GET[$var];
		if(!$val)
		{
			return false;
		}
		//判断格式是否正确
		if(!ereg('^[a-z][a-z\_0-9\/]+$',$val))
		{
			exit('error: Only letters, numbers and underscores!');
		}
		return $val;
	}
}

//用于调试用的信息
if(!function_exists("sys_debug"))
{
	function sys_debug()
	{
		$app = sys_init();
		$time_used = sys_time_used();
		$mysql_count = $app->db_count();
		$mysql_times = $app->db_times();
		$memory = sys_memory_used();
		$msg = "Processed in ".$time_used." second(s), mysql: ".$mysql_count." queries";
		$msg.= ", ".$mysql_times." second(s)";
		if($memory)
		{
			$msg.= ", memory: ".$memory;
		}
		//$msg.= ".";
		return $msg;
	}
}

//根据得到的数据执行二次MD5加密方案
//参数最多只支持一维数据（多维数据将会出错）
if(!function_exists("sys_md5"))
{
	function sys_md5($rs)
	{
		if(!$rs)
		{
			return false;
		}
		$rs = is_array($rs) ? implode("[:phpok:]",$rs) : $rs;
		return md5(md5($rs));
	}
}

//执行变量替换，以实现在语言包中使用变量
if(!function_exists("sys_eval"))
{
	function sys_eval($string,$val="")
	{
		if(!$val)
		{
			return $string;
		}
		if(is_array($val))
		{
			foreach($val AS $key=>$value)
			{
				$string = str_replace("{".$key."}",$value,$string);
			}
		}
		else
		{
			$string = preg_replace("/\{(.*)\}/isU",$val,$string);
		}
		return $string;
	}
}

if(!function_exists("sys_id_list"))
{
	function sys_id_list($id)
	{
		if(!$id || (!is_array($id) && !is_string($id)))
		{
			return false;
		}
		if(is_string($id))
		{
			$new_list = array();
			$idlist = explode(",",$id);
			foreach($idlist AS $key=>$value)
			{
				$value = trim($value);
				if($value)
				{
					$new_list[] = $value;
				}
			}
		}
		else
		{
			$new_list = array();
			foreach($id AS $key=>$value)
			{
				$value = trim($value);
				if($value)
				{
					$new_list[] = $value;
				}
			}
		}
		$new_list = array_unique($new_list);
		if(count($new_list)>0)
		{
			return $new_list;
		}
		else
		{
			return false;
		}
	}
}

//指格式化ID
if(!function_exists("sys_id_string"))
{
	function sys_id_string($id)
	{
		$id = sys_id_list($id);
		if($id)
		{
			return implode(",",$id);
		}
		else
		{
			return false;
		}
	}
}

if(!function_exists("sys_ip"))
{
	function sys_ip()
	{
		$cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
		$rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
		$fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
		$ip = "0.0.0.0";
		if($cip && $rip)
		{
			$ip = $cip;
		}
		elseif($rip)
		{
			$ip = $rip;
		}
		elseif($cip)
		{
			$ip = $cip;
		}
		elseif($fip)
		{
			$ip = $fip;
		}

		if (strstr($ip, ','))
		{
			$x = explode(',', $ip);
			$ip = end($x);
		}

		if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip))
		{
			$ip = '0.0.0.0';
		}

		unset($cip);
		unset($rip);
		unset($fip);

		return $ip;
	}
}

//读取当前分类下的子分类ID，递归法
if(!function_exists("sys_son_cateid"))
{
	function sys_son_cateid(&$cate_array,$id=0)
	{
		$sys_app = sys_init();
		$sys_app->load_model("category_model",true);
		$sys_app->category_model->langid($_SESSION["sys_lang_id"]);
		$rslist = $sys_app->category_model->get_son_cateid($id);
		if($rslist && is_array($rslist) && count($rslist))
		{
			foreach($rslist AS $key=>$value)
			{
				$cate_array[] = $value["id"];
				sys_son_cateid($cate_array,$value["id"]);
			}
		}
	}
}

//格式化价格
if(!function_exists("sys_format_price"))
{
	function sys_format_price($val="0.00",$currency="1.0000")
	{
		return number_format(round($val*$currency,2),2,".","");
	}
}

//格式化字符串
if(!function_exists("sys_cutstring"))
{
	function sys_cutstring($title,$length=255,$ext="...")
	{
		if(!$title)
		{
			return false;
		}
		$app = sys_init();
		return $app->trans_lib->cut($title,$length,$ext);
	}
}

if(!function_exists("sys_fckeditor"))
{
	function sys_fckeditor($var="",$defaultvalue="",$toolbar="Default",$height="370px",$width="690px")
	{
		include_once(LIBS."/system/fckeditor.php");
		$var = $var ? $var : "content";
		$fck = new FCKeditor($var) ;//获得一个变量信息
		$sBasePath = $_SERVER['PHP_SELF'] ;
		$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "_samples" ) );
		$fck->BasePath = $sBasePath."libs/editor/";
		$fck->Value = $defaultvalue;
		$fck->Config['AutoDetectLanguage'] = false;
		$fck->Config['DefaultLanguage'] = "zh-cn";
		$fck->Config['ToolbarStartExpanded'] = true;
		$fck->ToolbarSet = $toolbar == "Basic" ? "Basic" : "Default";
		$fck->Width = $width ? $width : "543px";
		$fck->Height = $height ? $height : "300px";
		$fck->Config['EnableXHTML'] = true;
		$fck->Config['EnableSourceXHTML'] = true;
		return $fck->CreateHtml();
	}
}

if(!function_exists("base_url"))
{
	function base_url()
	{
		$myurl = "http://".str_replace("http://","",$_SERVER["SERVER_NAME"]);
		$docu = $_SERVER["PHP_SELF"];
		$array = explode("/",$docu);
		$count = count($array);
		if($count>1)
		{
			foreach($array AS $key=>$value)
			{
				$value = trim($value);
				if($value)
				{
					if(($key+1) < $count)
					{
						$myurl .= "/".$value;
					}
				}
			}
		}
		$myurl .= "/";
		return $myurl;
	}
}

if(!function_exists("sys_write_cache_txt"))
{
	function sys_write_cache_txt()
	{
		$app = sys_init();
		$rslist = $this->cache_lib->cache_rs;
		$rslist = $app->cache_save();
		//echo "<pre>".print_r($rslist,true)."</pre>";
		$app->db->cache_write_txt($rslist);
		return true;
	}
}
?>